package com.zsh.weather.ln.web

import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.yechaoa.yutilskt.LogUtilKt
import com.yechaoa.yutilskt.ToastUtilKt
import com.zsh.weather.ln.R
import com.zsh.weather.ln.base.BaseActivity
import com.zsh.weather.ln.bean.CityEntry
import com.zsh.weather.ln.db.AppDatabase
import com.zsh.weather.ln.utils.Constant
import kotlinx.android.synthetic.main.activity_web_layout.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class WebActivity : BaseActivity() {
    private  var cityName: String = ""
    private  var fxLink: String = ""
    override fun initView() {
        initWebView()
        click()
    }

    private fun click() {
        iv_back.setOnClickListener {
            finish()
        }
        iv_add.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                LogUtilKt.d("cityName_:" + cityName + "|" + "fxLink_:" + fxLink)
                var cityEntry = CityEntry(cityName, fxLink)
                withContext(Dispatchers.IO) {
                    if (AppDatabase.getDatabase(mContext).cityDao()
                            .isExisy(cityEntry.name).size <= 0
                    )
                        AppDatabase.getDatabase(mContext).cityDao().insterCity(cityEntry)
                }
                iv_add.visibility = View.GONE
                ToastUtilKt.showToast("添加完成")
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_web_layout
    }

    fun initWebView() {
        mWebView.settings.javaScriptEnabled = true
        mWebView.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        mWebView.settings.databaseEnabled = true
        mWebView.settings.domStorageEnabled = true
        mWebView.webViewClient = MyWebView()
        mWebView.webChromeClient = MyWebViewSec();
        var url = Constant.home_url
        var location = intent.getStringExtra("location")
        if (!location.contains("http")) {
            url += location
        } else {
            tv_tip.visibility = View.VISIBLE
            cityName = intent.getStringExtra("name")
            fxLink = location
            LogUtilKt.d("cityName_:" + cityName)
            url = location
        }
        LogUtilKt.d("url_:" + url)
        mWebView.loadUrl(url);
    }

    private inner class MyWebView : WebViewClient() {

        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            address_looking_up.visibility = View.GONE
            mWebView.visibility = View.VISIBLE
            if (cityName.isNotBlank()) {
                iv_add.visibility = View.VISIBLE
            }
        }
    }

    private class MyWebViewSec : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
        }
    }
}